package com.huawei.appmarket.service.externalapi.view;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.KeyEvent;
import androidx.fragment.app.Fragment;
import com.huawei.appgallery.foundation.ui.framework.cardframe.fragment.TaskFragment.e;
import com.huawei.appmarket.support.emui.permission.BasePermissionActivity;
import com.huawei.secure.android.common.intent.SafeIntent;
import java.lang.ref.WeakReference;
import o.ayw;
import o.cjk;
import o.cjm;
import o.diu;
import o.dky.j;
import o.dky.k;
import o.Logger;
import o.doo;
import o.dym;
import o.dyr;
import o.dys;
import o.dyt;
import o.dyx;
import o.NetWorkUtls;
import o.ActivityUtil;
import o.evd;
import o.fiu;
import o.fjb;
import o.FragmentManager;
import o.FragmentTransaction;

public class ThirdApiActivity extends BasePermissionActivity implements o.dys.a, dyt {
    private Handler ˊॱ;
    private dyx ˏॱ;
    private dyr ͺ;
    private ProgressDialog ॱˊ;
    private a ᐝॱ;

    public static abstract class a implements e {
        protected String ˋ;
        protected o.dys.a ˏ;

        public a(String str, o.dys.a aVar) {
            this.ˋ = str;
            this.ˏ = aVar;
        }
    }

    static class d extends Handler {
        private final WeakReference<ThirdApiActivity> ॱ;

        private d(ThirdApiActivity thirdApiActivity) {
            this.ॱ = new WeakReference(thirdApiActivity);
        }

        public void handleMessage(Message message) {
            try {
                ThirdApiActivity thirdApiActivity = (ThirdApiActivity) this.ॱ.get();
                if (thirdApiActivity != null) {
                    dyr ˊॱ = thirdApiActivity.ˊॱ();
                    if (ˊॱ != null && !ˊॱ.onTimeout()) {
                        thirdApiActivity.finish();
                    }
                }
            } catch (Throwable e) {
                Logger.ˋ("ThirdApi", "timeout have error!", e);
            }
        }
    }

    protected void ᐝ() {
        requestWindowFeature(1);
        evd.ˏ(getWindow());
    }

    public void onCreate(Bundle bundle) {
        Logger.ˎ("ThirdApi", "onCreate");
        Object ab_ = ab_();
        if (ab_ instanceof a) {
            a aVar = (a) ab_;
            aVar.ˏ = this;
            this.ᐝॱ = aVar;
        }
        int identifier = getResources().getIdentifier("androidhwext:style/Theme.Emui", null, null);
        if (this.ᐝॱ == null) {
            ᐝ();
        } else if (identifier != 0) {
            setTheme(k.ˏ);
            setTheme(identifier);
            evd.ˋ(this, o.dky.d.ˋॱ, o.dky.d.ˏॱ);
        }
        if (identifier == 0) {
            setTheme(16973934);
        }
        this.ˏॱ = dym.ॱ();
        this.ˏॱ.ˎ((dyt) this, bundle);
        super.onCreate(bundle);
    }

    public void ॱॱ() {
        this.ͺ = dys.ˋ(this);
        if (this.ͺ == null) {
            finish();
            return;
        }
        Logger.ˎ("ThirdApi", "getAction:" + this.ͺ.getClass().getSimpleName());
        this.ˏॱ.ˎ((dyt) this, this.ͺ.useCacheProtocol());
    }

    public void am_() {
        if (this.ͺ != null) {
            this.ͺ.onPause();
        }
    }

    public void ʻ() {
        Logger.ˎ("ThirdApi", "onReject");
        setResult(1002, null);
        ॱˊ();
        if (this.ͺ != null) {
            this.ͺ.cancelTask();
        }
        finish();
    }

    public void ˋ() {
        if (this.ͺ != null) {
            if (dys.ˊ() != null) {
                dys.ˊ().ॱ(new SafeIntent(getIntent()), this.ͺ);
            }
            try {
                if (this.ᐝॱ == null && this.ͺ.isNeedLoading()) {
                    ͺ();
                } else {
                    ॱˊ();
                }
                long timeout = this.ͺ.getTimeout();
                if (timeout != -1) {
                    this.ˊॱ = new d();
                    this.ˊॱ.sendEmptyMessageDelayed(1, timeout);
                }
                if (!ॱˎ()) {
                    finish();
                } else if (this.ᐝॱ == null || doo.ˊ((Context) this)) {
                    this.ͺ.doAction();
                } else {
                    ॱ(this.ᐝॱ);
                }
            } catch (Exception e) {
                Logger.e("ThirdApi", "action error Exception!");
                finish();
            }
        }
    }

    private boolean ॱˎ() {
        if (!TextUtils.isEmpty(this.ͺ.getPermission())) {
            Object callingPackage = getCallingPackage();
            if (TextUtils.isEmpty(callingPackage)) {
                Logger.ˎ("ThirdApi", "can not get CallingPackage.");
                return false;
            } else if (getPackageManager().checkPermission(this.ͺ.getPermission(), callingPackage) != 0) {
                Logger.ˎ("ThirdApi", "Permission denial: require " + this.ͺ.getPermission());
                return false;
            }
        }
        return true;
    }

    public void onDestroy() {
        Logger.ˎ("ThirdApi", "onDestroy");
        if (this.ॱˊ != null && this.ॱˊ.isShowing()) {
            this.ॱˊ.dismiss();
        }
        if (this.ˊॱ != null) {
            this.ˊॱ.removeCallbacksAndMessages(null);
        }
        if (dys.ˊ() != null) {
            dys.ˊ().ˎ(new SafeIntent(getIntent()), this.ͺ);
        }
        super.onDestroy();
        if (this.ͺ != null) {
            this.ͺ.onDestroy();
        }
        diu.ˊ().ˋ(this);
        this.ˏॱ.ˋ(this);
    }

    public void ॱ(a aVar) {
        if (!ActivityUtil.isAcitivtyNotValid(this)) {
            Object obj = this.ᐝॱ == null ? 1 : null;
            this.ᐝॱ = aVar;
            ॱˊ();
            if (obj != null) {
                recreate();
                return;
            }
            Fragment noNetworkLoadingFragment = new NoNetworkLoadingFragment();
            noNetworkLoadingFragment.ˊ(aVar);
            FragmentManager ˏॱ = ˏॱ();
            FragmentTransaction ˊ = ˏॱ.ˊ();
            Fragment findFragmentByTag = ˏॱ.findFragmentByTag("NoNetworkLoadingFragment");
            if (findFragmentByTag != null) {
                ˊ.ˏ(findFragmentByTag);
            } else {
                ˊ.ˏ(16908290, noNetworkLoadingFragment, "NoNetworkLoadingFragment");
            }
            ˊ.ˋ();
        }
    }

    public void onSaveInstanceState(Bundle bundle) {
        super.onSaveInstanceState(bundle);
        this.ˏॱ.ॱ(this, bundle);
    }

    public void ͺ() {
        if (!ActivityUtil.isAcitivtyNotValid(ˊ())) {
            if (this.ॱˊ == null) {
                this.ॱˊ = new ProgressDialog(ˊ());
                this.ॱˊ.setMessage(getString(j.ʼᐝ));
                this.ॱˊ.setCanceledOnTouchOutside(false);
                this.ॱˊ.setOnCancelListener(new OnCancelListener(this) {
                    final /* synthetic */ ThirdApiActivity ˋ;

                    {
                        this.ˋ = r1;
                    }

                    public void onCancel(DialogInterface dialogInterface) {
                        Logger.e("ThirdApi", "cancel progressDialog");
                        this.ˋ.ʻ();
                    }
                });
                ॱˋ();
            } else if (!this.ॱˊ.isShowing()) {
                ॱˋ();
            }
        }
    }

    private void ॱˋ() {
        try {
            this.ॱˊ.show();
            ayw.ˊ(this.ॱˊ.getWindow());
        } catch (Throwable e) {
            Logger.ˋ("ThirdApi", "can not show dialog", e);
        }
    }

    public void ॱˊ() {
        if (this.ॱˊ != null) {
            this.ॱˊ.dismiss();
        }
    }

    public void startActivityForResult(Intent intent, int i) {
        ˋ(intent);
        try {
            super.startActivityForResult(intent, i);
        } catch (Throwable e) {
            Logger.ˋ("ThirdApi", "can not find activity", e);
            ˊ("main.activity");
        }
    }

    public void startActivity(Intent intent) {
        ˋ(intent);
        try {
            super.startActivity(intent);
        } catch (Throwable e) {
            Logger.ˋ("ThirdApi", "can not find activity", e);
            ˊ("main.activity");
        }
    }

    private void ˋ(Intent intent) {
        Object ˏ = ˏ();
        if (!TextUtils.isEmpty(ˏ)) {
            intent.putExtra("THIRD_APP_CALLER_PKG", ˏ);
        }
        try {
            intent.putExtras(new SafeIntent(getIntent()));
        } catch (Exception e) {
            Logger.e("ThirdApi", "addBasicParam error");
        }
    }

    public void ˎ(cjm o_cjm, int i) {
        try {
            Intent ॱ = o_cjm.ॱ(this);
            ˋ(ॱ);
            ॱ.addFlags(i);
            cjk.ˎ().ˋ(this, o_cjm);
        } catch (IllegalArgumentException e) {
            Logger.e("ThirdApi", "can not find uri,start MainActivity IllegalArgumentException");
            ˊ("main.activity");
        }
    }

    public void ˊ(String str) {
        ˎ(new cjm(str, null), 0);
    }

    public void ˋ(cjm o_cjm, int i) {
        ˋ(o_cjm.ॱ(this));
        try {
            cjk.ˎ().ˎ((Activity) this, o_cjm, i);
        } catch (ActivityNotFoundException e) {
            Logger.ˋ("ThirdApi", "ActivityNotFoundException :" + e.toString());
        }
    }

    public void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
        if (this.ͺ != null) {
            this.ͺ.onActivityResult(i, i2, intent);
        }
    }

    public Activity ˊ() {
        return this;
    }

    public void ˏ(fjb o_fjb, Intent intent) {
        if (o_fjb != null) {
            fiu.ˊ().ॱ((Context) this, o_fjb, intent);
        }
    }

    public String ˏ() {
        if ("4017125".equals(new SafeIntent(getIntent()).getStringExtra("thirdId"))) {
            return "com.android.packageinstaller";
        }
        return getCallingPackage();
    }

    @TargetApi(23)
    public void ˏ(int i, int i2) {
        String action = new SafeIntent(getIntent()).getAction();
        if ("com.huawei.appmarket.intent.action.PROTOCOL".equals(action)) {
            if (i2 != 0) {
                boolean shouldShowRequestPermissionRationale = shouldShowRequestPermissionRationale("android.permission.READ_PHONE_STATE");
                if (shouldShowRequestPermissionRationale) {
                    setResult(1002, null);
                    finish();
                    return;
                }
                if (this.ˋॱ == null) {
                    this.ˋॱ = new NetWorkUtls();
                }
                this.ˋॱ.showDial(this, shouldShowRequestPermissionRationale, ʿ(), 14);
                setResult(1002, null);
                return;
            }
        } else if ("com.huawei.appmarket.intent.action.ThirdUpdateAction".equals(action) && ˎ(i2)) {
            return;
        }
        super.ˏ(i, i2);
    }

    @TargetApi(23)
    private boolean ˎ(int i) {
        if (i == 0) {
            return false;
        }
        boolean shouldShowRequestPermissionRationale = shouldShowRequestPermissionRationale("android.permission.READ_PHONE_STATE");
        if (shouldShowRequestPermissionRationale) {
            setResult(4, null);
            finish();
            return true;
        }
        if (this.ˋॱ == null) {
            this.ˋॱ = new NetWorkUtls();
        }
        this.ˋॱ.showDial(this, shouldShowRequestPermissionRationale, ʿ(), 14);
        setResult(4, null);
        return true;
    }

    public dyr ˊॱ() {
        return this.ͺ;
    }

    public boolean onKeyDown(int i, KeyEvent keyEvent) {
        boolean z = true;
        if (i == 4 && keyEvent.getRepeatCount() == 0) {
            setResult(1002, null);
        }
        if (this.ͺ == null) {
            return false;
        }
        int onKeyDown = this.ͺ.onKeyDown(i, keyEvent);
        if (-1 == onKeyDown) {
            return super.onKeyDown(i, keyEvent);
        }
        if (onKeyDown != 1) {
            z = false;
        }
        return z;
    }

    public Object T_() {
        if (this.ᐝॱ != null) {
            this.ᐝॱ.ˏ = null;
        }
        return this.ᐝॱ;
    }
}
